package com.leyou.item.controller;


import com.leyou.item.pojo.SpecGroup;
import com.leyou.item.pojo.SpecParam;
import com.leyou.item.service.SpecService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("spec")
public class SpecController {

    @Autowired
    private SpecService specService;

    /**
     * 根据分类的id查询分类对应的规格组
     *
     * @param cid
     * @return
     */
    @GetMapping("groups/{cid}")
    public ResponseEntity<List<SpecGroup>> querySpecGroups(@PathVariable("cid") Long cid) {

        List<SpecGroup> spceGroups = specService.querySpecGroups(cid);
        if (spceGroups != null && 0 != spceGroups.size()) {
            return ResponseEntity.ok(spceGroups);
        }


        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();//204
    }


    /**
     * 根据分类的id，查询分类下面大的规格参数新的信息
     * 由于分类的id不是规格参数的主键，所以还是属性查询
     * @param gid
     * @return
     */
    @GetMapping("params")
    public ResponseEntity<List<SpecParam>> querySpecParam(@RequestParam(value = "gid",required = false) Long gid,
                                                          @RequestParam(value = "cid",required = false) Long cid,
                                                          @RequestParam(value = "searching",required = false)Boolean searching,
                                                          @RequestParam(value = "generic",required = false)Boolean generic
                                                          ) {
        List<SpecParam> specParams = specService.querySpecParam(gid,cid,searching,generic);
        if (specParams != null && 0 != specParams.size()) {
            return ResponseEntity.ok(specParams);
        }

        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }


}
